﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zhy.HelpClassLibrary.Utility.TxtHelper
{
    /// <summary>
    /// 文本文件读写操作类
    /// </summary>
    public class TextHelper
    {
        /// <summary>
        /// 向指定文本文件写入一行信息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="filePath"></param>
        public static void WriteMsg(string msg, string filePath, bool bAppend)
        {
            using (StreamWriter sw = new StreamWriter(filePath, bAppend)) // txt文件流写入
            {
                sw.WriteLine(msg); // 写入一行文本
            }
        }

        /// <summary>
        /// 向指定文本文件写入多行文本
        /// </summary>
        /// <param name="msgList"></param>
        /// <param name="filePath"></param>
        public static void WriteMsgList(List<string> msgList, string filePath, bool bAppend)
        {
            using (StreamWriter sw = new StreamWriter(filePath, bAppend)) // 文件流写入
            {
                foreach (string s in msgList) // 循环写入
                {
                    sw.WriteLine(s); // 向指定文件写入指定行数据
                }
            }
        }

        /// <summary>
        /// 读取文件所有行文本
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static List<string> ReadTextAllLines(string filePath)
        {
            List<string> lines = new List<string>();
            if (File.Exists(filePath))
            {
                lines = File.ReadLines(filePath).ToList(); // 多去txt文件所有行数据
            }
            return lines;
        }

    }
}
